//************** FUNCTIONS ******************
_CleanArray=
{
  _fname=_this select 0;
  call compile format["
  %1_Man=nil;
  %1_Land=nil;
  %1_Air=nil;
  %1_Ship=nil;
  %1_Static=nil;
  %1_Logistic=nil;
  %1_Animal=nil;
  %1_Group=nil;
  %1_GroupConfig=nil;
  %1_Object=nil;
  %1_Composition=nil;
  %1_CompositionScript=nil;
  %1_Empty=nil;
  %1_Base=nil;
  ",_fname];
};


_ShowProgress=
{
  _hint=("Loading :\n"+(_this select 0));
  hintsilent _hint;
};

_CreateFactionArray=
{
  _faction=_this select 0;
  _type=_this select 1;
  _data=_this select 2;
  _enforce=false;
  _cfgdata=nil;
  
  if (count _this >3) then {_enforce=_this select 3;};
  if (count _this >4) then {_cfgdata=_this select 4;};
  
  if !(_enforce) then
  {
      _allside=west_factions+east_factions+resistance_factions+civilian_factions;
      
      //Creating a global factions (same in each side) if the specified one in the config is not in side.
      if !(_faction in _allside) then
      {

        //Checking if the faction class as a specified side somewhere in its path. else, add the faction to all side *sigh* 
        _westcheck=true;
        _eastcheck=true;
        _resistancecheck=true;
        _civiliancheck=true;
        
        if !(isnil "_cfgdata") then
        {
          _cfgstring=format["%1",_cfgdata];        
          if (([_cfgstring,"west"] call KRON_StrInStr)) then {_westcheck=true;_eastcheck=false;_resistancecheck=false;_civiliancheck=false;};
          if (([_cfgstring,"east"] call KRON_StrInStr)) then {_westcheck=false;_eastcheck=true;_resistancecheck=false;_civiliancheck=false;};
          if (([_cfgstring,"guerrila"] call KRON_StrInStr)) then {_westcheck=false;_eastcheck=false;_resistancecheck=true;_civiliancheck=false;};
          if (([_cfgstring,"civilian"] call KRON_StrInStr)) then {_westcheck=false;_eastcheck=false;_resistancecheck=false;_civiliancheck=true;};
        };
        
        if (_westcheck) then {west_factions=west_factions+[count west_factions,_faction];}; 
        if (_eastcheck) then {east_factions=east_factions+[count east_factions,_faction];};
        if (_resistancecheck) then {resistance_factions=resistance_factions+[count resistance_factions,_faction];};
        if (_civiliancheck) then {civilian_factions=civilian_factions+[count civilian_factions,_faction];};
          
      };
  };
  call compile format["
  if (isnil ""%1_%2"") then
  {
    call compile format[""%1_%2=[];"",_faction];
    [""%1_%2""] call _ShowProgress;  
  };
  %1_%2 set [count %1_%2,_data];
  ",_faction,_type];  
};

_GetConfigName=
{
  _config=_this select 0;
  _txt=configName _config;
  _txt=toUpper([_txt," ",""] call KRON_Replace);
  _txt
};
_SpareSpace=
{
  _txt=_this select 0;
  _txt=toUpper([_txt," ",""] call KRON_Replace);
  _txt
};
//*****************************************


//Gentlemen, start your engine
_hint="Loading Ressource...";
hint _hint;



//Creating side arrays
west_factions=[];
east_factions=[];
resistance_factions=[];
civilian_factions=[];
//That one isn't generated dynamicly
object_factions=["Object"];

ShopLandList=[];
ShopAirList=[];
ShopStaticList=[];
ShopManList=[];

//First, i look at the factions per side 
_factions=configFile >> "CfgFactionClasses";

//Filling side arrays with factions
_n=(count _factions)-1;
//Starting at 1 to avoid the false class named (access) in cfgfactions
for "_i" from 0 to _n do
{
  if (isclass(_factions select _i)) then 
  {
    call compile format["
    _exist=[_factions select %1,""side""] call bis_fnc_returnconfigentry;
    if !(isnil ""_exist"") then {    
    if (getnumber ((_factions select %1) >> ""side"") == 1) then {west_factions=west_factions+[count west_factions,[(_factions select %1)] call _GetConfigName];[configName(_factions select %1)] call _CleanArray;};
    if (getnumber ((_factions select %1) >> ""side"") == 0) then {east_factions set [count east_factions,[(_factions select %1)] call _GetConfigName];[configName(_factions select %1)] call _CleanArray;};
    if (getnumber ((_factions select %1) >> ""side"") == 2) then {resistance_factions set [count resistance_factions,[(_factions select %1)] call _GetConfigName];[configName(_factions select %1)] call _CleanArray;}; 
    if (getnumber ((_factions select %1) >> ""side"") == 3) then {civilian_factions set [count civilian_factions,[(_factions select %1)] call _GetConfigName];[configName(_factions select %1)] call _CleanArray;};
    };
    ",_i];  
  };
  //hint format["%1",(configName(_factions select 4))];
};
["Object"] call _CleanArray;

//*****************************
//Deleting existing units arrays
//*****************************



//*****************************
//Creating units arrays (ouch)
//*****************************

_cfg=configFile >> "CfgVehicles";
_n=(count _cfg)-1;

for "_i" from 0 to _n do
{
  //Working class
  _class=_cfg select _i;
  
  
  if (isclass(_class)) then 
  {
    //*******************************
    //*** Special object unscoped ***
    //*******************************
    
    //Sound object
    /*
    if (([_class,"vehicleclass"] call bis_fnc_returnconfigentry)=="Sounds") then
    {
      GlobalSoundList=GlobalSoundList+[[configName(_class),configName(_class)]];   
    };    
    
    //********************************
    */
     
    //Veryfing scope (wich determine if it can be viewed in the editor
    _scope=[_class,"scope"] call bis_fnc_returnconfigentry;
    
    if (typename _scope != typename 0) then
    {
    	_scope = call compile _scope;
        
    };
    
    
    if (!(isnil "_scope") and (_scope>1) ) then
    {
      
      
      //Taking the classname
      _classname=configName(_class);
      //Taking Class type
      _classtype=[_class,"vehicleclass"] call bis_fnc_returnconfigentry;
      //*******************
      
      //Working with man
      if (_classname iskindof "Man") then
      {
        _faction=[_class,"faction"] call bis_fnc_returnconfigentry;
        if !(isnil "_faction") then 
        {
          _faction=[_faction] call _SpareSpace;
          [_faction,"Man",_classname,false,_class] call _CreateFactionArray;
          ShopManList set [count ShopManList,"""CfgVehicles"">>"""+_classname+""""];            
        };
      };
      /*
      //Working with animal
      if (_classname iskindof "Animal") then
      {
        _faction=[_class,"faction"] call bis_fnc_returnconfigentry;
        if !(isnil "_faction") then 
        {
          //_faction=[_faction] call _SpareSpace;
          _faction="Object";
          [_faction,"Animal",_classname,false,_class] call _CreateFactionArray;
          
        };
      };
      */
      
      //Working with Land vehicles
      if ((_classname iskindof "LandVehicle") and !(_classname iskindof "StaticWeapon")) then
      {
        _faction=[_class,"faction"] call bis_fnc_returnconfigentry;
        if !(isnil "_faction") then 
        {
          _faction=[_faction] call _SpareSpace;
          [_faction,"Land",_classname,false,_class] call _CreateFactionArray;
          [_faction,"Empty",_classname,false,_class] call _CreateFactionArray;    
          ShopLandList set [count ShopLandList,"""CfgVehicles"">>"""+_classname+""""];      
        };
      };
      
      //Working with air vehicles
      if (_classname iskindof "Air") then
      {
        _faction=[_class,"faction"] call bis_fnc_returnconfigentry;
        if !(isnil "_faction") then 
        {
          _faction=[_faction] call _SpareSpace;
          [_faction,"Air",_classname,false,_class] call _CreateFactionArray;
          [_faction,"Empty",_classname,false,_class] call _CreateFactionArray;    
          ShopAirList set [count ShopAirList,"""CfgVehicles"">>"""+_classname+""""];   
        };
        
      };  
  
      if (_classname iskindof "Ship") then
      {
        _faction=[_class,"faction"] call bis_fnc_returnconfigentry;
        if !(isnil "_faction") then 
        {
          _faction=[_faction] call _SpareSpace;
          [_faction,"Ship",_classname,false,_class] call _CreateFactionArray;
          [_faction,"Empty",_classname,false,_class] call _CreateFactionArray;
          //Only items with pictures are added to the shop
          if ([_class,"picture"] call  bis_fnc_returnconfigentry !="") then
          {        
            ShopLandList set [count ShopLandList,"""CfgVehicles"">>"""+_classname+""""];
          };   
        };
        
      };  
    
      //Working with static vehicles
      if (_classname iskindof "StaticWeapon") then
      {
        _faction=[_class,"faction"] call bis_fnc_returnconfigentry;
        if !(isnil "_faction") then 
        {
          _faction=[_faction] call _SpareSpace;
          [_faction,"Static",_classname,false,_class] call _CreateFactionArray;
          [_faction,"Empty",_classname,false,_class] call _CreateFactionArray;
          
          //Only disassemblable Static are listed in the shop
          if (isclass (_class >> "assembleInfo")) then
          {    
            ShopStaticList set [count ShopStaticList,"""CfgVehicles"">>"""+_classname+""""];
          };   
        };
        
      };  
      
      //********* SPECIFIC OBJECTS ***********
      
        
      //Ammobox doesn't have faction value.
       if ((_classtype=="Ammo") or (_classtype=="ACE_Ammunition") or (_classtype=="ACE_Ammunition_Rope")  or (_classtype=="ACE_Ammunition_CSW")  or (_classtype=="ACE_AmmunitionTransportCSW") or (_classtype=="ACE_AmmunitionTransportUS ") or (_classtype=="ACE_AmmunitionTransportRU") or (_classtype=="ACE_Ammunition_Ruck")) then
      {        
          _faction="Object";
          [_faction,"Logistic",_classname,true,_class] call _CreateFactionArray;    
       
      };
          
       //Miscobject also doesn't have faction value.
       if (_classtype=="Small_items") then
      {
          _faction="Object";
          [_faction,"Object",_classname,true,_class] call _CreateFactionArray;
      
      };
      
      //Camp_Fire, on request
      if (_classname iskindof "Land_Fire") then
      {
          _faction="Object";
          [_faction,"Object",_classname,true,_class] call _CreateFactionArray;
         
      };   
      
      //Forticiation, on request
      if (_classtype=="Fortifications") then
      {
           _faction="Object";
          [_faction,"Object",_classname,true,_class] call _CreateFactionArray;    
        
      }; 

      //Wrecks
      if (_classtype=="Wrecks") then
      {
           _faction="Object";
          [_faction,"Object",_classname,true,_class] call _CreateFactionArray;    
        
      };   
      
      //Deads
      if (_classtype=="Dead_bodies") then
      {
           _faction="Object";
          [_faction,"Object",_classname,true,_class] call _CreateFactionArray;    
        
      }; 

      //Deads
      if (_classtype=="Military") then
      {
           _faction="Object";
          [_faction,"Object",_classname,true,_class] call _CreateFactionArray;    
        
      }; 
          
      //IEDs   
      if (_classtype=="IEDs" ) then
      {
           _faction="Object";
          [_faction,"Object",_classname,true,_class] call _CreateFactionArray;    
          
      };    
      

      //End of Config work  
    };
    //End of Config Class check
  };
  //End of Config loop
};

//**** Working with Groups *********

_cfg=(configFile >> "CfgGroups");
_n=(count _cfg)-1;
for "_i" from 0 to _n do
{
  //Working class
  _class=_cfg select _i;
  
  _cfg2=(configFile >> "CfgGroups" >> configName(_class));
  _n2=(count _cfg2)-1;
  if (_n2<0) then {_n2=0;};
  //hint format["%1",_i];
  sleep 1;
  for "_i2" from 0 to _n2 do
  {
    _class2=_cfg2 select _i2;
    
      
    _cfg3=(configFile >> "CfgGroups" >> configName(_class) >> configName(_class2));
    _n3=(count _cfg3)-1;
    if (_n3<0) then {_n3=0;};
    for "_i3" from 0 to _n3 do
    {
      _class3=_cfg3 select _i3;
      
      _cfg4=(configFile >> "CfgGroups" >> configName(_class) >> configName(_class2) >> configName(_class3));
      _n4=(count _cfg4)-1;
      if (_n4<0) then {_n4=0;};
      for "_i4" from 0 to _n4 do
      {
        _class4=_cfg4 select _i4;
        if (isclass(_class4)) then
        { 
           //Taking the classname
            _classname=configName(_class4);
            //*******************
    
            //Working with Group
    
            _faction=[_class4,"faction"] call bis_fnc_returnconfigentry;
            if !(isnil "_faction") then 
            {
              _faction=[_faction] call _SpareSpace;
              [_faction,"Group",_classname,false,_cfg4] call _CreateFactionArray;
              [_faction,"GroupConfig",(configFile >> "CfgGroups" >> configName(_class) >> configName(_class2) >> configName(_class3) >> configName(_class4) ),false,_cfg4] call _CreateFactionArray; 
              
            };
        };
      
      };
      
    };
    
  };
    
};


//Parsing object composition.
_cfg=configFile >> "CfgObjectCompositions";
_n=(count _cfg)-1;

for "_i" from 0 to _n do
{
  //Working class
  _class=_cfg select _i;
  if (isclass(_class)) then 
  {  
    //Gettin class name
    _classname=configName(_class);
    
    //Checking this class as a objectscript and so is a composition class
    _objscript=[_class,"objectScript"] call bis_fnc_returnconfigentry;
    if !(isnil "_objscript") then
    {
      _faction="Object";    
     [_faction,"Composition",_classname,true,_class] call _CreateFactionArray;
     [_faction,"CompositionScript",_objscript,true,_class] call _CreateFactionArray;
    };      
  };
};


//Loading Weapons  
ShopWeaponList=[];
_cfg=(configFile >> "CfgWeapons");
_n=(count _cfg)-1;
//user1 sidechat format["%1",_n];
["Weapons"] call _ShowProgress;
for "_i" from 0 to _n do
{
  //Working class
  _class=_cfg select _i;
  //user1 sidechat format["%1",configName(_class)];
  if (isclass(_class)) then 
  {  
    //Gettin class name
    _classname=configName(_class);
    
    //Checking this class as a objectscript and so is a composition class
    _scope=[_class,"scope"] call bis_fnc_returnconfigentry;
    
    if (typename _scope != typename 0) then
    {
    	_scope = call compile _scope;
        
    };    
    
    _pic=[_class,"picture"] call bis_fnc_returnconfigentry;
    if (!(isnil "_scope") and (_scope>1) and !(isnil "_pic") and !(_pic=="")) then
    {        
      ShopWeaponList set [count ShopWeaponList,"""CfgWeapons"">>"""+_classname+""""];
    };

  };
};

//Loading Magazines
ShopMagazineList=[];
ShopArtyAmmoList=[];
_cfg=(configFile >> "CfgMagazines");
_n=(count _cfg)-1;
//user1 sidechat format["%1",_n];
["Magazines"] call _ShowProgress;
for "_i" from 0 to _n do
{
  //Working class
  _class=_cfg select _i;
  //user1 sidechat format["%1",configName(_class)];
  if (isclass(_class)) then 
  {  
    //Gettin class name
    _classname=configName(_class);
    
    //Checking this class as a objectscript and so is a composition class
    _scope=[_class,"scope"] call bis_fnc_returnconfigentry;
    
    if (typename _scope != typename 0) then
    {
    	_scope = call compile _scope;
        
    };    
    
    _pic=[_class,"picture"] call bis_fnc_returnconfigentry;
    if (!(isnil "_scope") and (_scope>1) and !(isnil "_pic") and !(_pic=="")) then
    {        
      ShopMagazineList set [count ShopMagazineList,"""CfgMagazines"">>"""+_classname+""""];
    };
    
    //Filling Artillery ammo list if Magazine is for Arty
    _arty=[_class,"arty_ballistics"] call bis_fnc_returnconfigentry;
    if ((_scope>1) and (_arty!="")) then
    {        
      //ShopArtyAmmoList set [count ShopArtyAmmoList,"""CfgAmmo"">>"""+_classname+""""];
      ShopArtyAmmoList set [count ShopArtyAmmoList,[[_class,"displayname"] call bis_fnc_returnconfigentry,[_class,"ammo"] call bis_fnc_returnconfigentry]];
    };    
  };
};



/*
//Loading Artilley ammo
ShopArtyAmmoList=[];
_cfg=(configFile >> "CfgAmmo");
_n=(count _cfg)-1;
//user1 sidechat format["%1",_n];
["Artillery Ammunitions"] call _ShowProgress;
for "_i" from 0 to _n do
{
  //Working class
  _class=_cfg select _i;
  //user1 sidechat format["%1",configName(_class)];
  if (isclass(_class)) then 
  {  
    //Gettin class name
    _classname=configName(_class);
    
    //Checking if the ammo is some kind of artillery and as FX
    
    _arty=[_class,"arty_trailfx"] call bis_fnc_returnconfigentry;
 
    _effect=[_class,"explosioneffects"] call bis_fnc_returnconfigentry;
    if (isnil "_effect") then {_effect="";};
    
    _model=[_class,"model"] call bis_fnc_returnconfigentry;
    if (isnil "_model") then {_model="";};
    
    if (!(isnil "_arty") and (_model!="") and (_effect!="")) then
    {        
      //ShopArtyAmmoList set [count ShopArtyAmmoList,"""CfgAmmo"">>"""+_classname+""""];
      ShopArtyAmmoList set [count ShopArtyAmmoList,_classname,_classname];
    };
  };
};
*/
//hintc format["%1",ShopArtyAmmoList];

//Loading musics 
GlobalMusicList=[];
_cfg=configFile >> "CfgMusic";
_n=(count _cfg)-1;
["Musics"] call _ShowProgress;
for "_i" from 0 to _n do
{
  //Working class
  _class=_cfg select _i;
  if (isclass(_class)) then 
  {  
    //Gettin class name
    _classname=configName(_class);
    
    //Checking this class as a objectscript and so is a composition class
    _name=[_class,"name"] call bis_fnc_returnconfigentry;
    if !(isnil "_name") then
    {
      _loc=[_name,1] call KRON_StrLeft;
      if (_loc=="$") then 
      {
        _namecount=[_name] call KRON_StrLen;
        _name=localize([_name,(_namecount-1)] call KRON_StrRight);
      };  
    }
    else
    {
    _name=_classname;
    }; 
    GlobalMusicList=GlobalMusicList+[[_name,_classname]];     
  };
};

//Loading Sounds 
GlobalSoundList=[];
_cfg=(configFile >> "CfgSFX");
_n=(count _cfg)-1;
//user1 sidechat format["%1",_n];
["Sounds"] call _ShowProgress;
for "_i" from 0 to _n do
{
  //Working class
  _class=_cfg select _i;
  //user1 sidechat format["%1",configName(_class)];
  if (isclass(_class)) then 
  {  
    //Gettin class name
    _classname=configName(_class);
    
    //Checking this class as a objectscript and so is a composition class
    _name=[_class,"name"] call bis_fnc_returnconfigentry;
    
    if !(isnil "_name") then
    {
      _loc=[_name,1] call KRON_StrLeft;
      if (_loc=="$") then 
      {
        _namecount=[_name] call KRON_StrLen;
        _name=localize([_name,(_namecount-1)] call KRON_StrRight);
      };  
    }
    else
    {
    _name=_classname;
    };
    if (_name=="") then {_name=_classname;};
    
    GlobalSoundList=GlobalSoundList+[[_name,_classname]];     
  };
};


//Detecting if Combined Arms is installed, if not remove the A2 factions from side
//Removing factions that don't have units (bad config, override or anything that doesn't need to be in the menu)
["Cleaning factions"] call _ShowProgress;

_cleanside=
{
_side=_this select 0;
_nf=(count _side)-1;
_i=0;
_nfremove=[];
for "_i" from 0 to _nf do
{
  _nkeep=false;
  _nfaction=_side select _i;
  {call compile format["if !(isnil ""%2_%1"") then {_nkeep=true;};",_x,_nfaction];} foreach globalcamp_types;
  if !(_nkeep) then {_nfremove=_nfremove+[_nfaction];};
};
_side=_side-_nfremove;
_side
};

west_factions=[west_factions] call _cleanside;
east_factions=[east_factions] call _cleanside;
resistance_factions=[resistance_factions] call _cleanside;
civilian_factions=[civilian_factions] call _cleanside;



//Here we add specific stuff to each faction
all_factions=west_factions+east_factions+resistance_factions+civilian_factions;

_n=(count all_factions)-1;
_i=0;
private "_script";
for "_i" from 0 to _n do
{
  _faction=all_factions select _i;
  
  if !(_faction in object_factions) then {call compile format["%1_Base=[""Checkpoint"",""Outpost""];",_faction]};

}; 

all_factions=west_factions+east_factions+resistance_factions+civilian_factions+object_factions;


//***************************************************************************
//Broadcasting array to everyone (so the script can be run on the server side)
//***************************************************************************
{
  _n=(count globalcamp_types)-1;
  for "_i" from 0 to _n do
  {
    call compile format["
    if !(isnil ""%1_%2"") then 
    {
      publicvariable ""%1_%2"";
      if (""%2""==""Group"") then { publicvariable ""%1_GroupConfig""};
      if (""%2""==""Composition"") then { publicvariable ""%1_CompositionScript""};
    };
    ",_x,(globalcamp_types select _i)];  
  };
} foreach all_factions;

publicvariable "west_factions";
publicvariable "east_factions";
publicvariable "resistance_factions";
publicvariable "civilian_factions";
publicvariable "object_factions";
publicvariable "GlobalMusicList";
publicvariable "GlobalSoundList";
publicvariable "ShopLandList";
publicvariable "ShopAirList";
publicvariable "ShopStaticList";
publicvariable "ShopManList";
publicvariable "ShopWeaponList";
publicvariable "ShopMagazineList";
publicvariable "ShopArtyAmmoList";


//********************************************
//Finishing log
//********************************************
